LCP 67. 装饰树


https://leetcode.cn/problems/KnLfVT/description/




struct TreeNode*BuyNewnod()
{
    struct TreeNode*newnode=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    if(newnode==NULL)
    {
        perror("malloc fail\n");
        return ;
    }
    newnode->left=NULL;
    newnode->right=NULL;
    newnode->val=-1;
    return newnode;
}

struct TreeNode* expandBinaryTree(struct TreeNode* root){
    if(root==NULL)
    {
        return NULL;
    }
    if(!root->left&&!root->right)
    {
        return root;
    }
    if(root->left)
    {
        struct TreeNode*tmp=root->left;
        struct TreeNode*newnode=BuyNewnod();
        root->left=newnode;
        newnode->left=tmp;
        newnode->left=expandBinaryTree(newnode->left);
    }

    if(root->right)
    {
        struct TreeNode*tmp=root->right;
        struct TreeNode*newnode=BuyNewnod();
        root->right=newnode;
        newnode->right=tmp;
        newnode->right=expandBinaryTree(newnode->right);
    }

    return root;
}